package com.tim.app.ui.popup;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.application.library.runtime.event.EventManager;
import com.tim.app.R;
import com.tim.app.RT;
import com.tim.app.constant.AppKey;
import com.tim.app.constant.EventTag;


/**
 * Created by fanxiaofeng on 16-4-6.
 */
public class SelectDynamicPop extends PopupWindow implements View.OnClickListener {

    private Context mContext;
    private View rootView;
    private TextView tvAllDynamic, tvFriendDynamic, tvCancel;

    public View getRootView() {
        return rootView;
    }

    public SelectDynamicPop(Context context, int type) {
        super(context);
        this.mContext = context;
        setWidth(RT.getScreenWidth());
        setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
        setFocusable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable());

        rootView = LayoutInflater.from(context).inflate(R.layout.pop_select_dynamic, null);
        setContentView(rootView);

        tvAllDynamic = (TextView) rootView.findViewById(R.id.tvAllDynamic);
        tvFriendDynamic = (TextView) rootView.findViewById(R.id.tvFriendDynamic);
        tvCancel = (TextView) rootView.findViewById(R.id.tvCancel);
        tvAllDynamic.setOnClickListener(this);
        tvFriendDynamic.setOnClickListener(this);
        tvCancel.setOnClickListener(this);
        if (type == AppKey.DYNAMIC_TYPE_ALL) {
            tvAllDynamic.setSelected(true);
        } else if (type == AppKey.DYNAMIC_TYPE_FOLLOWER) {
            tvFriendDynamic.setSelected(true);
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvAllDynamic:
                EventManager.ins().sendEvent(EventTag.ON_DYNAMIC_ALL, 0, 0, null);
                dismiss();
                break;
            case R.id.tvFriendDynamic:
                EventManager.ins().sendEvent(EventTag.ON_DYNAMIC_FRIEDN, 0, 0, null);
                dismiss();
                dismiss();
                break;
            case R.id.tvCancel:
                dismiss();
                break;
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        backgroundAlpha(1f);
    }

    public void show(View parent) {
        if (!isShowing()) {
            this.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
            backgroundAlpha(0.5f);
        }
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        ((Activity) mContext).getWindow().setAttributes(lp);
    }

}

